<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of hoidap_traloi_model
 *
 * @author Bui
 */
class Hoidap_traloi_model extends CI_Model {

    private $tbl_hoidap_traloi = 'gs7s_hoidap_traloi';
	private $tbl_hoidap = 'gs7s_hoidap';

    //put your code here
    public function __construct() {
        parent::__construct();
		$this->load->database();
    }

    public function load_comment($id_comment) {
        $this->db->select('gs7s_thanhvien.username AS username, gs7s_hoidap_traloi.noidung AS noidung_comment, gs7s_hoidap.tieude AS tieude , gs7s_hoidap.noidung AS noidung_cauhoi');
        $this->db->from('gs7s_hoidap_traloi');
        $this->db->where('gs7s_hoidap_traloi.id', $id_comment);
        $this->db->join('gs7s_hoidap','gs7s_hoidap.id = gs7s_hoidap_traloi.hoidap_id' );
        $this->db->join('gs7s_thanhvien', 'gs7s_thanhvien.id = gs7s_hoidap_traloi.thanhvien_id');
        /*
        Produces:
        SELECT gs7s_thanhvien.username, gs7s_hoidap_traloi.noidung, gs7s_hoidap.tieude, gs7s_hoidap.noidung
        FROM gs7s_hoidap_traloi
        WHERE gs7s_hoidap_traloi.id= $id_comment
        JOIN gs7s_hoidap ON gs7s_hoidap.id = gs7s_hoidap_traloi.hoidap_id
        JOIN gs7s_thanhvien ON gs7s_thanhvien.id = gs7s_hoidap_traloi.thanhvien_id
         */
        return $query = $this->db->get();
    }
    function load_comment_by_question($id_hoidap) {
        $this->db->select('gs7s_thanhvien.username, ' . $this->tbl_hoidap_traloi . '.noidung');
        $this->db->from($this->tbl_hoidap_traloi);
        $this->db->where($this->tbl_hoidap_traloi . '.hoidap_id', $id_hoidap);
        $this->db->join('gs7s_thanhvien', 'gs7s_thanhvien.id=' . $this->tbl_hoidap_traloi . '.thanhvien_id');
        $this->db->order_by($this->tbl_hoidap_traloi.'.id','DESC');
        return $query = $this->db->get();
    }

    function pcreate_comment($data) {
        // Insert comment to table "gs7s_hoidap_traloi"
        $user = '';
        $this->db->insert($this->tbl_hoidap_traloi, $data);
        $this->db->select('username');
        $this->db->from('gs7s_thanhvien');
        $this->db->where('id', $data['thanhvien_id']);
        $query = $this->db->get();
        foreach ($query->result() as $row) {
            $user = $row->username;
        }
        return $this->returnMarkup($user, $data['noidung']);
    }

    function returnMarkup($name, $mess) {
        return '<div class="panel panel-default" style="padding: 5px; margin: 1px;">
                    <div class="panel-body" style="padding-top: 0px; padding-bottom: 0px; margin: 2px;">
                <div class="col-lg-1" style="padding-left: 0px;">
                      <img src="'.base_url().'/image/hoidap_detail/user'.rand(1, 3).'.png" height="40" width="40">
                </div>
                <div class="col-lg-11">
                <strong>' . $name . '</strong><br>' .
                $mess . '</div></div></div>';
    }

    function delete_comment($id) {
        // Delete comment has id = '$id'
        $this->db->delete('gs7s_hoidap_traloi', array('id' => $id)); 
       // Produces:
       // DELETE FROM tbl_hoidap_traloi 
       // WHERE id = $id
    }

    function load_all_comments() {
        $this->db->select('gs7s_hoidap_traloi.id, gs7s_hoidap_traloi.noidung, gs7s_hoidap.tieude, gs7s_thanhvien.username');
        $this->db->from('gs7s_hoidap_traloi');
        $this->db->join('gs7s_hoidap', 'gs7s_hoidap_traloi.hoidap_id = gs7s_hoidap.id');
        $this->db->join('gs7s_thanhvien', 'gs7s_hoidap_traloi.thanhvien_id = gs7s_thanhvien.id');
        $this->db->order_by('gs7s_hoidap_traloi.id', "desc");
        return $query = $this->db->get();
        /*
        Produces:
        SELECT gs7s_hoidap_traloi.id, gs7s_hoidap_traloi.noidung, gs7s_hoidap.tieude, gs7s_thanhvien.username
        FROM gs7s_hoidap_traloi
        JOIN gs7s_hoidap ON gs7s_hoidap_traloi.hoidap_id = gs7s_hoidap.id
        JOIN gs7s_thanhvien ON gs7s_hoidap_traloi.thanhvien_id = gs7s_thanhvien.id
        ORDER BY gs7s_hoidap_traloi.id, "desc"
         */
    }
	// Use for pagination
    // Lets you limit the number of rows you would like returned by the query
    public function fetch_data($limit, $start,$query) {//ok
        $this->db->limit($limit, $start);
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
   // User for pagination
   // Count the number of rows 
   public function record_count($query) {//ok
        return $query->num_rows();
    }


}

?>
